package com.gosophia.reportData.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.entity.DocumentInfo;
import com.gosophia.conferenceManagement.common.service.ConferenceFileService;
import com.gosophia.conferenceManagement.common.ws.ConferenceQueryWebService;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.dataDictionary.ws.DataDictionaryWebService;
import com.gosophia.flowFormManagement.dto.FlowFormDTO;
import com.gosophia.flowFormManagement.ws.FlowFormWebService;
import com.gosophia.reportData.dto.ExcelDataEntityDTO;
import com.gosophia.reportData.dto.LaunchReportJobDTO;
import com.gosophia.reportData.ws.ExcelDataProcessWebService;
import com.gosophia.reportData.ws.LaunchReportWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.userManagement.service.UserManagementService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 发起上报action
 * 
 * @author tianchunyu
 * 
 */
@Controller
@Scope("prototype")
public class StartReportAction extends BaseSupportAction {
	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	@Autowired
	private LaunchReportWebService launchReportWebService;

	@Autowired
	private UserManagementService UserManagementService;

	@Autowired
	private DataDictionaryWebService dataDictionaryWebService;

	@Autowired
	private FlowFormWebService flowFormWebService;

	@Autowired
	private ConferenceFileService conferenceFileService;

	@Autowired
	private ConferenceQueryWebService conferenceQueryWebService;

	@Autowired
	private ExcelDataProcessWebService excelDataProcessWebService;

	private MissionDTO missionDto;

	private LaunchReportJobDTO launchReportJobDto;

	// 通讯录
	private List<Long> userInfoIds;

	// 附件
	private List<File> files;
	private List<String> filesFileName;
	private List<String> description;

	private List<Long> oldDocumentIds;

	// 来源标记
	private String fromFlag;

	// excel报送数据
	private String onlinePath;
	private String onlineVersion;
	private int dataRow;

	@Override
	@LogRecord(eventAction = "数据报送--报送任务发起", eventTarget = "launchReportJobDto")
	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long launchUserId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();

		// 生成附件
		// 返回的附件赋值组装用于保存到数据库
		List<Document> returnDocuments = new ArrayList<Document>();
		List<DocumentDTO> oldDocumentList = new ArrayList<DocumentDTO>();
		List<DocumentDTO> returnDocDTO = new ArrayList<DocumentDTO>();
		if (files != null && files.size() != 0) {
			for (int i = 0; i < files.size(); i++) {
				Document document = new Document();
				document.setDocumentName(filesFileName.get(i));
				document.setDocumentNote(description.get(i));
				DocumentInfo documentInfo = new DocumentInfo();
				documentInfo.setDocumentBasicInfo(document);
				FileInputStream documentContent = new FileInputStream(files
						.get(i));
				documentInfo.setDocumentContent(documentContent);
				// 组装返回的附件信息
				Document returnDoc = conferenceFileService
						.saveDocumentData(documentInfo);
				returnDoc.setDocumentName(filesFileName.get(i));
				returnDoc.setDocumentNote(description.get(i));
				returnDoc.setDocumentCategory("attachment");
				returnDocuments.add(returnDoc);
			}
			returnDocDTO = changeDocumentToDTO(returnDocuments);
		}
		if (oldDocumentIds != null && oldDocumentIds.size() != 0) {
			for (Long id : oldDocumentIds) {
				DocumentDTO d = conferenceQueryWebService
						.findDocumentByDocumentId(id);
				oldDocumentList.add(d);
			}
		}
		if (oldDocumentList != null && oldDocumentList.size() != 0) {
			returnDocDTO.addAll(oldDocumentList);
		}
		launchReportJobDto.setAttachmentDtos(returnDocDTO);

		// 表单上报数据
		if (launchReportJobDto.getReportMethod().equals("B")) {
			FlowFormDTO flowFormDto = flowFormWebService
					.findFlowFormById(launchReportJobDto.getFlowFormDto()
							.getFormId());
			launchReportJobDto.setFlowFormDto(flowFormDto);
		}

		// excel上报数据
		ExcelDataEntityDTO ex = new ExcelDataEntityDTO();
		if (launchReportJobDto.getReportMethod().equals("E")) {
			ex.setOnlinePath(onlinePath);
			ex.setOnlineVersion(onlineVersion);
			ex.setDataRow(dataRow);
			if (launchReportJobDto.getJobId() != 0) {
				ex.setJobId(launchReportJobDto.getJobId());
			}
			ex.setOperator(UserManagementService
					.findUserInfoByUserInfoId(launchUserId));
			ex.setEffect("Launch");
			excelDataProcessWebService.save(ex);
			launchReportJobDto.setExcelDataEntityDto(ex);
		}

		launchReportJobDto.setUserInfoIds(userInfoIds);

		DataValueDTO importanceDTO = dataDictionaryWebService
				.findDataValueByDataValueId(missionDto.getImportanceDTO()
						.getDataValueId());

		missionDto.setImportanceDTO(importanceDTO);

		// 发起数据上报方法
		MissionDTO mDto = launchReportWebService.createLaunchReport(missionDto,
				launchReportJobDto, launchUserId);
		if(ex.getJobId()==null){
			ex.setJobId(mDto.getFirstJobDTO().getJobId());
		}

		String reportChannel = launchReportJobDto.getReportChannel();
		if ("submit".equals(reportChannel)) {
			super.getRequestResult().setMessage(
					"上报任务名称为" + missionDto.getMissionName() + ", 任务编号为" + mDto.getMissionCode()
							+ " 的任务已提交审批成功");
		} else if ("sure".equals(reportChannel)) {
			super.getRequestResult().setMessage(
					"上报任务名称为" + missionDto.getMissionName() + ", 任务编号为" + mDto.getMissionCode()
							+ " 的任务已发起成功");
		}
		super.getRequestResult().setRsltCode("0");
		ActionContext.getContext().getSession().put("_lastOpResult",
				super.getRequestResult());
		if ("task".equals(fromFlag)) {
			return "TASK";
		} else {
			return SUCCESS;
		}
	}

	public List<Long> getUserInfoIds() {
		return userInfoIds;
	}

	public void setUserInfoIds(List<Long> userInfoIds) {
		this.userInfoIds = userInfoIds;
	}

	public List<File> getFiles() {
		return files;
	}

	public void setFiles(List<File> files) {
		this.files = files;
	}

	public List<String> getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(List<String> filesFileName) {
		this.filesFileName = filesFileName;
	}

	public MissionDTO getMissionDto() {
		return missionDto;
	}

	public void setMissionDto(MissionDTO missionDto) {
		this.missionDto = missionDto;
	}

	public LaunchReportJobDTO getLaunchReportJobDto() {
		return launchReportJobDto;
	}

	public void setLaunchReportJobDto(LaunchReportJobDTO launchReportJobDto) {
		this.launchReportJobDto = launchReportJobDto;
	}

	public List<String> getDescription() {
		return description;
	}

	public void setDescription(List<String> description) {
		this.description = description;
	}

	public String getFromFlag() {
		return fromFlag;
	}

	public void setFromFlag(String fromFlag) {
		this.fromFlag = fromFlag;
	}

	public List<Long> getOldDocumentIds() {
		return oldDocumentIds;
	}

	public void setOldDocumentIds(List<Long> oldDocumentIds) {
		this.oldDocumentIds = oldDocumentIds;
	}

	public String getOnlinePath() {
		return onlinePath;
	}

	public void setOnlinePath(String onlinePath) {
		this.onlinePath = onlinePath;
	}

	public int getDataRow() {
		return dataRow;
	}

	public void setDataRow(int dataRow) {
		this.dataRow = dataRow;
	}

	private List<DocumentDTO> changeDocumentToDTO(List<Document> oldDoc) {
		List<DocumentDTO> newDoc = new ArrayList<DocumentDTO>();
		if (oldDoc != null) {
			for (Document document : oldDoc) {
				DocumentDTO d = new DocumentDTO();
				d.setDocumentCategory("attachement");
				d.setDocumentId(document.getDocumentId());
				d.setDocumentName(document.getDocumentName());
				d.setDocumentNote(document.getDocumentNote());
				d.setDocumentPath(document.getDocumentPath());
				d.setDocumentVersion(document.getDocumentVersion());
				newDoc.add(d);
			}
		}
		return newDoc;
	}

}
